<?php


namespace app\api\controller\Goods;

use think\Model;
use think\Request;

trait Speci
{
    /** 
     * @var array
     */
    protected $field = ['name', 'sort', 'status', 'default', 'website', 'des'];
    /**
     * @var \app\api\model\Goods\Speci\Speci
     */
    protected $model;
    /**
     * @param Request $request
     * @param Model $model
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function list(Request $request)
    {
        /*$request->has('name','params', true) ? $where[] = ['name', 'LIKE', '%' . $request->param('name') . '%']
            : $request->has('website','params', true) ? $where[] = ['website', 'LIKE', '%' . $request->param('website')]
            : $where = [];*/
        $data =$request->param();
        $where = [];
        if (isset($data['name'])) {
            $where[] = ['name', 'LIKE', '%' . $data['name'] . '%'];
        }
        return $this->returnJson(
            200,
            'ok',
            $this->model->index($where,$data['page'],$data['size'])
        );
    }

    public function getAllBrand(Request $request)
    {
        return $this->returnJson('200','ok',$this->model->getAllBrand());
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function add(Request $request)
    {
        $data = $request->only($this->field);
        return $this->returnJson(200, 'ok', $this->model->add($data));
    }

    /**
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id)
    {
        return $this->returnJson(200, 'ok', $this->model->info($id));
    }

    /**
     * @param $id
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit($id, Request $request)
    {
        $data = $request->only($this->field);
        return $this->returnJson(200, 'ok', $this->model->edit($id, $data));
    }

    /**
     * @param int $id 属性id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($id)
    {
        return $this->model->del($id) ? $this->returnJson(200, 'ok') : $this->returnJson(500, '属性已绑定商品,无法删除');
    }
}
